/**
 * 流转引擎
 */
function Engine(params) {

	this.params = params;
	this.workId=params.workId;
	this.todoId=params.todoId;
	this.appId=params.appId;
	this.currentActivity=params.activity;
	this.readOnly=params.readOnly;
	this.menuCtl=params.menuCtl;

	var menuCtlObj=Util.strToObj(this.menuCtl);

	this.modules = new Array();

	var menuFlags={};

	if(!this.readOnly){

		if(menuCtlObj.save){
			this.formModule = new FormModule("#wf-form", this);
			this.modules.push(this.formModule);
			menuFlags.save=true;
		}

		//{"archives":false,"attach":true,"back":false,"complete":true,"delete":false,"opinion":false,"save":true}
		if(menuCtlObj.complete){//提交

			this.formModule = new FormModule("#wf-form", this);
			this.modules.push(this.formModule);

			menuFlags.submit=true;
			this.outComeModule = new OutComeModule(null, this);
			this.modules.push(this.outComeModule);

			this.actorModule = new ActorModule(null, this);
			this.modules.push(this.actorModule);
		}


		if(menuCtlObj.edit){
			//编辑公文正文
			this.editDocModule = new EditDocModule(null, this);
			this.modules.push(this.editDocModule);
			menuFlags.editDoc=true;
		}
	}

	var opnCtl=menuCtlObj==null?!this.readOnly:menuCtlObj.opinion;

	this.approvalModule = new ApprovalModule("",opnCtl,this);
	this.modules.push(this.approvalModule);



	var attachmentCtl=menuCtlObj==null?false:menuCtlObj.attach;
	this.attachmentModule = new AttachmentModule("#attachmentTable",attachmentCtl, this);
	this.modules.push(this.attachmentModule);

	this.flowLogModule = new FlowLogModule(null, this);
	this.modules.push(this.flowLogModule);
	menuFlags.flowLog=true;

	this.toolbarModule = new ToolbarModule("#wf-toolbar",menuFlags,this);
	this.modules.push(this.toolbarModule);

	this.init();
}

Engine.prototype.getModules = function() {
	return this.modules;
};

Engine.prototype.drive = function() {
	var form = this.formModule;
	var modules = this.getModules();
	var allOk=true;
	for (var i = 0; i < modules.length; i++) {
		var module = modules[i];
		allOk=allOk&&module.isOk();
		if (!module.isOk()) {//
			module.alert();
			break;
		}
	}
	if(allOk)
		form.submit();
};

Engine.prototype.complete = function() {
	this.toolbarModule.disableItem("save-menu");
	this.toolbarModule.disableItem("submit-menu");
};

Engine.prototype.addModule=function(module){
	this.modules.push(this.attachmentModule);
};

Engine.prototype.refresh = function() {
	//window.opener.location.reload();
};

Engine.prototype.init = function() {
	for ( var index in this.modules) {
		this.modules[index].init();
	}
};
